﻿using System;
using System.Web.Mvc;
using MSAcademic.v2.Infrastructure.Login;
using MSAcademic.v2.Infrastructure.User;
using MSAcademic.v2.Web.Areas.Admin.Models.User;

namespace MSAcademic.v2.Web.Areas.Admin.Controllers
{
    public partial class UserController : Controller
    {
        public ActionResult Update()
        {
            var viewModel = _userUpdateViewModelFactory();
            if (viewModel.SignedInUser == null)
            {
                return RedirectToRoute("Home");
            }

            viewModel.Id = viewModel.SignedInUser.Id;

            viewModel.Username = viewModel.User.Username;

            viewModel.FirstName = viewModel.User.FirstName;
            viewModel.LastName = viewModel.User.LastName;
            viewModel.Description = viewModel.User.Description;
            viewModel.Email = viewModel.User.Email;
            viewModel.FacebookLink = viewModel.User.FacebookLink;
            viewModel.TwitterLink = viewModel.User.TwitterLink;
            viewModel.OtherLink = viewModel.User.OtherLink;
            viewModel.LinkedInLink = viewModel.User.LinkedInLink;

            ViewData["CurrentView"] = "Profile";
            return View(viewModel);
        }
        [HttpPost, ValidateInput(false), ValidateAntiForgeryToken]
        public ActionResult Update(UserUpdateViewModel viewModel)
        {
            ViewData["CurrentView"] = "Profile";

            if (!ModelState.IsValid) return View(viewModel);
            if (viewModel.Username != viewModel.User.Username)
            {
                if (!viewModel.IsUsernameValid)
                {
                    viewModel.Username = viewModel.User.Username;
                    viewModel.ErrorMessage = "Korisničko ime se već koristi, molim odaberite drugo";
                    return View(viewModel);
                }
            }

            _userCommands.UpdateProfile(viewModel.Id, viewModel.Username, viewModel.Password, viewModel.FirstName, viewModel.LastName, viewModel.Description, viewModel.Email, viewModel.FacebookLink, viewModel.TwitterLink, viewModel.LinkedInLink, viewModel.OtherLink);
            if (viewModel.Username != viewModel.User.Username)
            {
                _loginService.SignOut();
                return RedirectToRoute("AdminSignIn");

            }
            return RedirectToRoute("AdminHome");
        }

    }
    public partial class UserController
    {
        private readonly Func<UserUpdateViewModel> _userUpdateViewModelFactory;
        private readonly IUserCommands _userCommands;
        private readonly ILoginService _loginService;

        public UserController(Func<UserUpdateViewModel> userUpdateViewModelFactory,
            IUserCommands userCommands,
            ILoginService loginService
           )
        {

            _loginService = loginService;
            _userCommands = userCommands;
            _userUpdateViewModelFactory = userUpdateViewModelFactory;


        }
    }
}
